package com.loongtech.bi.action.ios;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.loongtech.bi.entity.ios.EntityProductManager;
import com.loongtech.bi.manager.ios.ProductManager;
import com.loongtech.core.util.Utils;

@Controller("productManagerAction")
@Scope("view")
public class ProductManagerAction {

	@Autowired
	private ProductManager productManager;

	private List<EntityProductManager> list;

	private EntityProductManager entity;

	private Map<String, String> projectItmes; // 标示下拉列表

	public Map<String, String> getProjectItmes() {
		return projectItmes;
	}

	public void setProjectItmes(Map<String, String> projectItmes) {
		this.projectItmes = projectItmes;
	}

	@Autowired
	public void init() {
		initData();
	}

	private void initData() {
		entity = new EntityProductManager();
		projectItmes = productManager.getProjectMap();
		search();
	}

	public void search() {
		List<Object> cons = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer(" from EntityProductManager l where 1=1 ");
		if (!Utils.isEmptyStr(entity.getProjectid())) {
			hql.append(" and l.projectid = ? ");
			cons.add(entity.getProjectid());
		}
		if (!Utils.isEmptyStr(entity.getProductid())) {
			hql.append(" and l.productid = ? ");
			cons.add(entity.getProductid());
		}
		if (!Utils.isEmptyStr(entity.getName())) {
			hql.append(" and l.name = ? ");
			cons.add(entity.getName());
		}
		if (!Utils.isEmptyStr(entity.getPay())) {
			hql.append(" and l.pay = ? ");
			cons.add(entity.getPay());
		}
		if (!Utils.isEmptyStr(entity.getDetail())) {
			hql.append(" and l.detail = ? ");
			cons.add(entity.getDetail());
		}
		list = productManager.findByQuery(hql.toString(), cons.toArray());
	}

	public void add() {
		FacesContext context = FacesContext.getCurrentInstance();
		try {
			if (validate(context, entity)) {
				productManager.persist(entity);
				context.addMessage(null, new FacesMessage("添加成功！"));
				initData();
			}
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("添加失败！"));
		}
	}

	public void modify(EntityProductManager e) {
		FacesContext context = FacesContext.getCurrentInstance();
		try {
			if (validate(context, e)) {
				productManager.merge(e);
				context.addMessage(null, new FacesMessage("修改成功！"));
				initData();
			}
		} catch (Exception ex) {
			context.addMessage(null, new FacesMessage("修改失败！"));
		}
	}

	public void delete(int id) {
		FacesContext context = FacesContext.getCurrentInstance();
		try {
			if (!Utils.isEmptyStr(String.valueOf(id))) {
				productManager.removeById(id);
			}
			context.addMessage(null, new FacesMessage("删除成功！"));
			initData();
		} catch (Exception ex) {
			context.addMessage(null, new FacesMessage("修改失败！"));
		}
	}

	private boolean validate(FacesContext context, EntityProductManager ebp) {
		if (Utils.isEmptyStr(ebp.getProjectid())) {
			context.addMessage(null, new FacesMessage("工程id不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getProductid())) {
			context.addMessage(null, new FacesMessage("产品id不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getName())) {
			context.addMessage(null, new FacesMessage("名称不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getPay())) {
			context.addMessage(null, new FacesMessage("成本不能为空！"));
			return false;
		}
		return true;
	}

	public List<EntityProductManager> getList() {
		return list;
	}

	public void setList(List<EntityProductManager> list) {
		this.list = list;
	}

	public EntityProductManager getEntity() {
		return entity;
	}

	public void setEntity(EntityProductManager entity) {
		this.entity = entity;
	}

}
